<?php
namespace Modules\Activity\Http\Controllers\Admin\Jika;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Jika\Activity;
// use \Modules\Activity\Entities\ActivityRecord;
use Setting;

use Validator;
use ThrowException;
use DB;
use Cache;

class ActivityController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
            // 'type' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $options['order_by'] = ['list_weight' => 'desc'];
        // $options['with_count'] = ['tasks.skus'];
        $options['with'] = ['tasks.skus'];

        $options['only'] = [
            'id',
            'uuid',
            'created_at',
            'title',
            'money_price',
            'score_price',
            'stock',
            'sales',
            'status',
            'visit_total',
            'union_status_text',
            'union_status',
            'thumb',
            'list_weight',
            'tasks'
        ];

        $list = Activity::getList($options);

        // $list->each(function ($item) {
        //     $item->task->skus_count = $item->task->skus()->count();
        //     $item->task->swap_records_count = $item->swap_records()->count();
        // });

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        // 
        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    public function store(Request $req)
    {
        $rule = [
            'title' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $info = $req->all();


        DB::beginTransaction();

        $info['status'] = 0;
        $item = Activity::create($info);

        // $item->updateBoxRelation();

        // 创建示例奖品
        // $item->createExampleSkus();

        // 创建示例卡片
        // $item->createExampleCards();

        DB::commit();

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'update') {
            $info = $req->input('attributes');

            $item->update($info);
        }

        // $item->updateBoxRelation();
        // $item->updateStock();

        DB::commit();

        return [];
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }

}
